package com.huilian.iotbox.admin.wap.controller;


import com.huilian.iotbox.admin.wap.service.CommodityGroupService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CommodityDto;
import com.huilian.iotbox.data.dto.CommodityGroupDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.CommodityGroup;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/commodityGroup")
public class CommodityGroupController extends UserInfoController{

    @Autowired
    private CommodityGroupService commodityGroupService;

    @GetMapping("/getDefaultCommodityGroup")
    public ServerResponse getDefaultCommodityGroup(CommodityGroupDto commodityGroupDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId())== 0) {
            commodityGroupDto.setUserId(userInfo.getParentId());
        } else {
            commodityGroupDto.setUserId(userInfo.getId());
        }
        return commodityGroupService.getDefaultCommodityGroup(commodityGroupDto);
    }

    @PostMapping("/findByUserIdAndType")
    public ServerResponse findByUserIdAndType(@RequestBody CommodityGroupDto commodityGroupDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityGroupDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(commodityGroupService.findByUserIdAndType(commodityGroupDto));
    }

    @PostMapping("/insertOrUpdate")
    public ServerResponse insertOrUpdate(@RequestBody CommodityGroup commodityGroup, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId())== 0) {
            commodityGroup.setUserId(userInfo.getParentId());
        } else {
            commodityGroup.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(commodityGroupService.insertOrUpdate(commodityGroup));
    }
    @PostMapping("/deleteById")
    public ServerResponse deleteById(@RequestBody CommodityGroupDto commodityGroupDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityGroupDto.setUserInfo(userInfo);
        return commodityGroupService.deleteById(commodityGroupDto);
    }

    @PostMapping("/findCommodityGroupList")
    public ServerResponse findCommodityGroupList(@RequestBody CommodityGroupDto commodityGroupDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityGroupDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(commodityGroupService.findByUserIdAndType(commodityGroupDto));
    }
}
